package cc.fs.zhihuapp.ui.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.facebook.drawee.view.SimpleDraweeView;

import cc.fs.zhihuapp.R;
import cc.fs.zhihuapp.model.NewsHtml;
import cc.fs.zhihuapp.rest.asynchttp.Api;
import cc.fs.zhihuapp.ui.activity.NewsActivity;
import cc.fs.zhihuapp.ui.adapter.NewDetailsAdapter;
import cc.fs.zhihuapp.ui.base.BaseFragment;
import cc.fs.zhihuapp.utils.MyCallback;
import cc.fs.zhihuapp.utils.QuickReturnUtils;

/**
 * Created by fostion on 2015/9/6.
 */
public class NewDetailFragment extends BaseFragment {

    private RelativeLayout headView;
    private RecyclerView recyclerView;
    private SimpleDraweeView image;
    private LinearLayoutManager layoutManager;
    private NewsActivity parent;
    private NewDetailsAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_news_detail, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        headView = (RelativeLayout)view.findViewById(R.id.headview);
        recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
        image = (SimpleDraweeView)view.findViewById(R.id.image);
        parent = (NewsActivity)getActivity();
        layoutManager = new LinearLayoutManager(parent);
        adapter = new NewDetailsAdapter();

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                float scrollY = -QuickReturnUtils.getScrollY(recyclerView, 1) * 0.7f;
                headView.setTranslationY(scrollY);
            }
        });

        Api.getNewsDetail(parent.getNewsId(),new MyCallback.NetworkCallback0<NewsHtml>() {
            @Override
            public void success(NewsHtml newsHtml) {
                parent.getToolbar().setTitle(newsHtml.getTitle());
                //将 css 和 js 文件加入html中
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("<head>")
                        .append("<link href=\"")
                        .append((newsHtml.getCss()[0]).toString()).append("\"  rel=\"stylesheet\" type=\"text/css \n")
                        .append("<script language=\"JavaScript\" src=\"")
                        .append((newsHtml.getJs())[0].toString())
                        .append("\"> </script>")
                        .append("</head>");
                stringBuilder.append(newsHtml.getBody());
                image.setImageURI(Uri.parse(newsHtml.getImage()));
                adapter.getWebView().loadDataWithBaseURL(null, stringBuilder.toString(), "text/html", "UTF-8", null);
            }

            @Override
            public void failure(String error) {

            }
        });

//        REST.api.getNewsDetail(parent.getNewsId(), new Callback<NewsHtml>() {
//                    @Override
//                    public void success(NewsHtml newsHtml, Response response) {
//
//
//                    }
//
//                    @Override
//                    public void failure(RetrofitError error) {
//
//                    }
//                }
//        );
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

}
